package com.movitech.mobile.cz.base.sys.service.impl;

import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.sys.entity.SCompany;
import com.movitech.mobile.cz.base.sys.entity.SProject;
import com.movitech.mobile.cz.base.sys.entity.SRegionalCompany;
import com.movitech.mobile.cz.base.sys.entity.ext.SCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SProjectRequestVO;
import com.movitech.mobile.cz.base.sys.mapper.SProjectMapper;
import com.movitech.mobile.cz.base.sys.service.SCompanyService;
import com.movitech.mobile.cz.base.sys.service.SProjectService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.List;

import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 项目信息 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sProjectService")
@Transactional
public class SProjectServiceImpl extends ServiceImpl<SProjectMapper, SProject> implements SProjectService {

	@Autowired
	SProjectMapper sProjectMapper;
	@Autowired
	SCompanyService sCompanyService;

	public List<SProject> getSProjectByCodeAndRegionalCompanyId(String code,String regionalCompanyId) {
		SProject searchSProject = new SProject();
		searchSProject.setProjectCode(code);
		if(StringUtils.isNotEmpty(regionalCompanyId))searchSProject.setRegionalCompanyId(regionalCompanyId);
		EntityWrapper<SProject> wrapper = new EntityWrapper<SProject>();
        wrapper.setEntity(searchSProject);
        return selectList(wrapper);
	}

	@Override
	public List<SProject> getSProjectByCodeAndRegionalCompanyId(String code, String regionalCompanyId, String companyId) {
		SProject searchSProject = new SProject();
		searchSProject.setProjectCode(code);
		if(StringUtils.isNotEmpty(regionalCompanyId))searchSProject.setRegionalCompanyId(regionalCompanyId);
		if(StringUtils.isNotEmpty(companyId))searchSProject.setCompanyId(companyId);
		EntityWrapper<SProject> wrapper = new EntityWrapper<SProject>();
        wrapper.setEntity(searchSProject);
        return selectList(wrapper);
	}

	@Override
	public SProject selectByCode(String code) {
		return sProjectMapper.selectByCode(code);
	}

	@Override
	public SProject selectByStageCode(String code) {
		return sProjectMapper.selectByStageCode(code);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
	@Override
	public void batchAdd(List<SProjectRequestVO> sprojectList) {
		try {
			if (sprojectList != null && sprojectList.size() > 0) {
				for (SProjectRequestVO sProjectRequestVO : sprojectList) {
					if (StringUtils.isEmpty(sProjectRequestVO.getProjectCode())) {
						continue;
					}
					SCompany sCompany = sCompanyService.selectByProjectFatherCode(sProjectRequestVO.getFatherCode());
					SProject sProject = this.selectByCode(sProjectRequestVO.getProjectCode());
					if (sProject == null) {
						sProject = new SProject();
						sProject.setProjectCode(sProjectRequestVO.getProjectCode());
						sProject.setProjectName(sProjectRequestVO.getProjectName());
						sProject.setCompanyId(sCompany.getId());
						sProject.setRegionalCompanyId(sCompany.getRegionalCompanyId());
						sProject.setBaseFeilds(UserUtils.getCurrentUserId(),"",sProjectRequestVO.getStatus(),true);
						this.insert(sProject);
					} else {
						if (sProject.getProjectCode().equals(sProjectRequestVO.getProjectCode())) {
							sProject.setProjectName(sProjectRequestVO.getProjectName());
							sProject.setCompanyId(sCompany.getId());
							sProject.setRegionalCompanyId(sCompany.getRegionalCompanyId());
							sProject.setBaseFeilds(UserUtils.getCurrentUserId(),"",sProjectRequestVO.getStatus(),false);
							this.updateById(sProject);
						}
					}
				}
			}
		} catch (RuntimeException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}
